package org.answer.service.impl;

import org.answer.dao.MenuDao;
import org.answer.pojo.Menu;
import org.answer.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findAll(String username) {
        //查询父菜单
        List<Menu> list = menuDao.findFirstMenu(username);
        for (Menu menu : list) {
            //根据父菜单id查询子菜单
            List<Menu> secondMenu = menuDao.findSecondMenu(username, menu.getId().toString());
            //将查询出的子菜单添加到对应父菜单中
            menu.setChildren(secondMenu);
        }
        return list;
    }


    @Override
    public List<Integer> findById(Integer id) {
        //获取角色可见的菜单id集合
        List<Integer> list = menuDao.findById(id);
        return list;
    }

    @Override
    public void updateById(Integer id, Integer[] ids) {
        //删除角色可见的菜单
        menuDao.deleteMenu(id);
        for (Integer menuid : ids) {
            //添加新的可见菜单
            menuDao.addMenu(id, menuid);
        }
    }
}
